<script>
	import {
		mapMutations,mapState 
	} from 'vuex';
	export default {
		computed: {
			...mapState(['hasLogin', 'userInfo', 'market'])
		},
		onLaunch: async function() {			
			// 设置响应拦截器
			console.log('App onLaunch1')
			var that = this;
			this.$request.interceptors.response(res => {
				uni.hideLoading();
				console.log('responseresponseresponse',res)
				if (res.statusCode == 200) {
					console.log('response------data:', res.data)
					if (res.data.code == 0) {
						return res.data.data || {};
					} else {
						if (res.data.data && res.data.data.code == 3000) {
							console.log('未登录==>', res.data);
							that.logout();
							that.wxlogin();
						} else {
							this.$api.msg(res.data.message || '系统繁忙，请稍后重试')
							return res.data;
						}
					}
				} else if(res.data) {
					this.$api.msg( res.data.message || '系统繁忙，请稍后重试')
				} else {
					this.$api.msg('网络异常')
				}
			});
		},
		onShow: function() {
			console.log('App onLaunch')
			console.log('App Show', uni.getStorageSync("token"), uni.getStorageSync("userInfo"))
			if (!uni.getStorageSync("token") || !uni.getStorageSync("userInfo")) {
				this.wxlogin()
			}

		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			...mapMutations(['login', 'logout', 'selectMarket']),
			async wxlogin() {
				let res = await uni.login({
					provider: 'weixin'
				});
				console.log('wxlogin,onload in..', res, res[1]['code']);
				if (res[1] && res[1]['errMsg'] == 'login:ok' && res[1]['code']) {
					let jscode = res[1]['code'];
					uni.setStorageSync('sessionCode', jscode);
					console.log('code', jscode);
					let res1 = await this.$request.get(this.$requestUrl.JSCONDE2_SESSION_URL, {
						loading: false,
						data: {
							code: jscode
						}
					});
					
					console.log('res1==>', res1);
					if (res1 && res1.token && res1.userInfo) {
						this.login(res1);
						console.log("userInfo",res1.userInfo)
						var market = {"name":"-","id": res1.userInfo.marketId}
						this.selectMarket(market)
					} else if (res1 && res1.session3rdKey) {
						uni.setStorageSync('session3rdKey', res1.session3rdKey);
						uni.setStorageSync('openid', res1.openid);
						if(!this.market || !this.market.id){
							console.log("market empty--->",this.market)
							uni.showModal({
								title: '提示',
								content: '请选择所处市场',
								success: function(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: '../pfsc/pfsc'
										});
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}
						
					}
				}
			}
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import './common/common.css';
	@import './common/iconfont.css';

	.uni-picker-container .uni-picker-action.uni-picker-action-confirm {
		color: #FF690D;
	}
</style>
